package com.yyfax.ph.progress.supplement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.widget.TextView;
import butterknife.BindView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.home.recommendloan.creditinfo.UploadCreditPaperFragment;
import com.yyfax.ph.main.MainActivity;
import com.yyfax.ph.progress.detail.ProgressDetailModel;

/**
 * 补充资料
 */
public class SupplementActivity extends BaseYYFaxActivity implements SupplementPresenter.View {
	private static final int LOADER_ID = 301;
	public static final String EXTRA_BEAN = "extra_bean";
	@BindView(R.id.current_status_tv)
	TextView mCurrentStatusTv;
	@BindView(R.id.reason_tv)
	TextView mReasonTv;
	private SupplementPresenter mPresenter;
	private DialogBase mCancelDialog;
	private DialogBase mExistDialog;
	private ProgressDetailModel.DetailBean mDetailBean;
	Fragment mFragment;

	public static void start(Activity aty, ProgressDetailModel.DetailBean detailBean) {
		Intent intent = new Intent(aty, SupplementActivity.class);
		intent.putExtra(SupplementActivity.EXTRA_BEAN, detailBean);
		aty.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBtnBackVisible(true);
		setTitleVisible(true);
		setTitle(R.string.supplement_information);
		setBtnRightTx(R.string.stop_recommend);
		mDetailBean = getIntent().getParcelableExtra(EXTRA_BEAN);
		mCurrentStatusTv.setText(mDetailBean.status);
		mReasonTv.setText(mDetailBean.reason);
		UploadCreditPaperFragment.ExtraParamsToUploadCredit credit = new UploadCreditPaperFragment.ExtraParamsToUploadCredit();
		if (TextUtils.equals(Constants.NEED_SUPPLEMENT_CREDIT, mDetailBean.supplementType)) {
			credit.bizType = UploadCreditPaperFragment.BIZ_TYPE_HAS_CREDIT_SUPPLEMENT_UPLOAD;
		} else {
			credit.bizType = UploadCreditPaperFragment.BIZ_TYPE_NO_CREDIT_SUPPLEMENT_UPLOAD;
		}
		credit.rcmId = mDetailBean.rcmdId;
		mFragment = UploadCreditPaperFragment.newInstance(credit);
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().
				replace(R.id.upload_credit_fl, mFragment);
		transaction.commit();
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new SupplementPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		if (mPresenter == null) {
			mPresenter = (SupplementPresenter) data;
			mPresenter.attach(this);
		}
	}

	@Override
	public int getContentViewRes() {
		return R.layout.supplement_activity_layout;
	}

	@Override
	public void onRightBtnPressed() {
		super.onRightBtnPressed();
		StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_D010);
		if (null == mCancelDialog) {
			mCancelDialog = new DialogBase(this, getString(
					R.string.cancel_quit_recommend_tips), null, this
					.getResources().getString(R.string.stop_recommend),
					getResources().getString(R.string.save_recommend_strs),
					new DialogBase.BottomTwoClickListener() {

						@Override
						public void onCancelClick(DialogBase dialog) {
							dialog.dismiss();
							StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_D011);
							mPresenter.cancelRecommend(mDetailBean.rcmdId);
						}

						@Override
						public void onConfirmClick(DialogBase dialog) {
							StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_D012);
							dialog.dismiss();
						}
					});
		}
		mCancelDialog.show();
	}

	@Override
	public void onCancelSuccess(String tips) {
		showToast(tips);
		MainActivity.startToProgressSearch(this);
	}

	@Override
	public void onCancelFailure(String tips) {
		showToast(tips);
	}

	@Override
	public void onCancelFailureExit(String tips) {
		if (mExistDialog == null) {
			mExistDialog = new DialogBase(this, tips, null, getString(
					R.string.confirm), new DialogBase.BottomOneClickListener() {
				@Override
				public void bottomOneClick(DialogBase dialog) {
					dialog.dismiss();
					MainActivity.startToProgressSearch(SupplementActivity.this);//取消进件失败表示推荐状态有更新，退回到进度查询界面需要刷新
				}

			}, false);
		}
		mExistDialog.show();
	}

	@Override
	public void onUploadSuccess(String tips) {

	}

	@Override
	public void onUploadFailure(String tips) {

	}

	@Override
	public void onUploadFailureExit(String tips) {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (mFragment != null) {
			mFragment.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	public void pretreatmentAfterSetBaseView() {
		super.pretreatmentAfterSetBaseView();
	}

	@Override
	public void initContentView() {
		super.initContentView();
	}

}
